package com.lvshou.medicalkit.module.main;

import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.common.libhttp.result.HttpResponseException;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.lvshou.medicalkit.R;
import com.lvshou.medicalkit.base.BaseActivity;
import com.lvshou.medicalkit.utils.BleUtil;

import java.util.List;

import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;

/**
 * MainActivity
 * @author Administrator
 * @Date   2019/3/6
 */ 
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View{

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.imageview)
    ImageView imageview;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

        getPresenter().checkAppVersion();
//        HttpClient.getApiService().getMeetingInfo().compose(bindLifecycle())
//                .compose(ResultTransformer.transformer())
//                .subscribe(new BaseObserver<String>() {
//
//                    @Override
//                    protected void onSuccess(String info) {
//                        Log.d("MainActivity", info);
//                    }
//
//                    @Override
//                    protected void onFailed(HttpResponseException responseException) {
//                        super.onFailed(responseException);
//                        Log.d("MainActivity", responseException.getMessage());
//                    }
//
//
//                });

        toolbar.setTitle("基础Demo");
//        Glide.with(this)
//                .load(R.drawable.ic_launcher_foreground)
//                .into(imageview);
        BleUtil.getInstance().setContext(this);
        BleUtil.getInstance().setBTUtilListener(new BleUtil.BTUtilListener() {
            @Override
            public void onLeScanStart() {

            }

            @Override
            public void onLeScanStop() {

            }

            @Override
            public void onLeScanDevices(List<BluetoothDevice> listDevice) {

            }

            @Override
            public void onConnected(BluetoothDevice mCurDevice) {

            }

            @Override
            public void onDisConnected(BluetoothDevice mCurDevice) {

            }

            @Override
            public void onConnecting(BluetoothDevice mCurDevice) {

            }

            @Override
            public void onDisConnecting(BluetoothDevice mCurDevice) {

            }

            @Override
            public void onStrength(int strength) {

            }

            @Override
            public void onModel(int model) {

            }
        });
        BleUtil.getInstance().scanLeDevice(true);
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public void onSuccess(String tag, Object info) {
        LogUtils.d(info.toString() );
        Toast.makeText(this, info.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(HttpResponseException error) {
        LogUtils.d(error.getMessage());
    }

    public static void resetPreferredLauncherAndOpenChooser(Context context) {
        PackageManager packageManager = context.getPackageManager();
        ComponentName componentName = new ComponentName(context, MainActivity.class);
        packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

        Intent selector = new Intent(Intent.ACTION_MAIN);
        selector.addCategory(Intent.CATEGORY_HOME);
        selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(selector);

        packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
    }
}
